#ifndef PHONERECORD_H
#define PHONERECORD_H

#include <QString>
#include <QAbstractListModel>
#include <QList>
#include <QListView>
#include <QVBoxLayout>
#include <QWidget>
#include <QStyledItemDelegate>
#include <QPainter>
//电话记录
class CallRecord {
public:
    CallRecord(const QString& name, const QString& number, const QString& time, const QString& duration)
        : name(name), number(number), time(time), duration(duration) {}

    QString name;
    QString number;
    QString time;
    QString duration;
};


//设置model
class CallRecordModel : public QAbstractListModel {
    Q_OBJECT
public:
    CallRecordModel(QObject* parent = nullptr) : QAbstractListModel(parent) {}

    enum CallRecordRoles {
        NameRole = Qt::UserRole + 1,
        NumberRole,
        TimeRole,
        DurationRole
    };

    void addRecord(const CallRecord& record) {
        beginInsertRows(QModelIndex(), rowCount(), rowCount());
        m_records.append(record);
        endInsertRows();
    }

    int rowCount(const QModelIndex& parent = QModelIndex()) const override {
        return m_records.count();
    }

    QVariant data(const QModelIndex& index, int role) const override {
        if (!index.isValid())
            return QVariant();

        const CallRecord& record = m_records.at(index.row());

        switch (role) {
            case NameRole:
                return record.name;
            case NumberRole:
                return record.number;
            case TimeRole:
                return record.time;
            case DurationRole:
                return record.duration;
            default:
                return QVariant();
        }
    }

    QHash<int, QByteArray> roleNames() const override {
        QHash<int, QByteArray> roles;
        roles[NameRole] = "name";
        roles[NumberRole] = "number";
        roles[TimeRole] = "time";
        roles[DurationRole] = "duration";
        return roles;
    }

private:
    QList<CallRecord> m_records;
};

//创建视图



class CallLogView : public QWidget {
    Q_OBJECT
public:
    CallLogView(QWidget* parent = nullptr) : QWidget(parent) {
        m_view = new QListView(this);
        m_model = new CallRecordModel(this);

        m_view->setModel(m_model);
        m_view->setViewMode(QListView::ListMode);
        m_view->setSpacing(10);

        // 添加一些假数据
        m_model->addRecord(CallRecord("John Doe", "123-456-7890", "2025-02-25 10:30", "5:30"));
        m_model->addRecord(CallRecord("Jane Smith", "987-654-3210", "2025-02-24 16:20", "3:12"));

        QVBoxLayout* layout = new QVBoxLayout(this);
        layout->addWidget(m_view);
        setLayout(layout);
    }

private:
    QListView* m_view;
    CallRecordModel* m_model;
};


//创建代理


class CallRecordDelegate : public QStyledItemDelegate {
    Q_OBJECT
public:
    CallRecordDelegate(QObject* parent = nullptr) : QStyledItemDelegate(parent) {}

    void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override {
        if (!index.isValid())
            return;

        QString name = index.data(CallRecordModel::NameRole).toString();
        QString number = index.data(CallRecordModel::NumberRole).toString();
        QString time = index.data(CallRecordModel::TimeRole).toString();
        QString duration = index.data(CallRecordModel::DurationRole).toString();

        // Custom painting
        QStyleOptionViewItem opt = option;
        initStyleOption(&opt, index);
        painter->fillRect(opt.rect, opt.palette.color(QPalette::Base));

        QFont font = painter->font();
        font.setBold(true);
        painter->setFont(font);

        painter->drawText(opt.rect, Qt::AlignTop | Qt::AlignLeft, name);
        painter->setFont(QFont());
        painter->drawText(opt.rect, Qt::AlignTop | Qt::AlignLeft, number);

        QRect timeRect = opt.rect;
        timeRect.moveTop(timeRect.top() + 40);
        painter->drawText(timeRect, Qt::AlignLeft, time);

        QRect durationRect = opt.rect;
        durationRect.moveTop(durationRect.top() + 60);
        painter->drawText(durationRect, Qt::AlignLeft, duration);
    }
};




#endif // PHONERECORD_H
